


from flask import Blueprint
from flask import request
from flask import jsonify
import psycopg2.extras
from database import db_pool_singleton
from database.user_info_table_method import UserInfoTableMethod
# from database.user_token_table_method import UserTokenTableMethod
# from database.user_login_log_table_method import UserLoginLogTableMethod
from database.user_operation_log_table_method import UserOperationLogTableMethod
from database.user_info_ext_table_method import UserInfoExtTableMethod

sign_up_blueprint = Blueprint('sign_up', __name__)


@sign_up_blueprint.route('/register', methods=['POST'])
def register():
    username = request.form.get('username')
    phone = request.form.get('phone')
    password = request.form.get('password')
    if not all([username, phone, password]):
        args_error = {
            "status": -1,
            "errorCode": 2001,
            "errorMsg": "args error",
            "resultBody": {}
        }
        return jsonify(args_error)

    with db_pool_singleton.getconn() as conn:
        cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)

        user_info_table_method = UserInfoTableMethod(conn, cur)

        # 判断用户是否存在
        user_info = user_info_table_method.select_user_info_by_username_or_phone(username, phone)
        if user_info:
            args_error = {
                "status": -1,
                "errorCode": 2002,
                "errorMsg": "user already exists",
                "resultBody": {}
            }
            return jsonify(args_error)
        

        # 插入用户信息
        user_id = user_info_table_method.insert_user_info(username, phone, password)


        # 插入用户操作日志
        user_operation_log_table_method = UserOperationLogTableMethod(conn, cur)
        user_operation_log_table_method.insert_user_operation_log(user_id, '注册', '注册成功')

        args_success = {
            "status": 1,
            "errorCode": None,
            "errorMsg": None,
            "resultBody": {
                "user_id": user_id,
                "username": username,
                "phone": phone,
                "account_status": 'normal'
            }
        }

        return jsonify(args_success)
    


# 添加和更新扩展用户信息
@sign_up_blueprint.route('/insert_user_info_ext', methods=['POST'])
def insert_user_info_ext():

    user_id = request.form.get('user_id')
    real_name = request.form.get('real_name')
    email = request.form.get('email')
    address = request.form.get('address')
    birthday = request.form.get('birthday')
    sex = request.form.get('sex')
    avatar = request.form.get('avatar')

    if not all([user_id, real_name, email, address, birthday, sex, avatar]):
        args_error = {
            "status": -1,
            "errorCode": 2003,
            "errorMsg": "args error",
            "resultBody": {}
        }
        return jsonify(args_error)
    
    with db_pool_singleton.getconn() as conn:
        cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)


        user_info_table_method = UserInfoTableMethod(conn, cur)
        user_info_ext_table_method = UserInfoExtTableMethod(conn, cur)

        # 判断用户是否存在
        user_info = user_info_table_method.select_user_info_by_id(user_id)
        if not user_info:
            args_error = {

                "status": -1,
                "errorCode": 2004,
                "errorMsg": "user not exists",
                "resultBody": {}
            }
            return jsonify(args_error)
        

        # 插入用户信息扩展
        user_info_ext_table_method.insert_user_info_ext(user_id, real_name, email, address, birthday, sex, avatar)

        # 插入用户操作日志
        user_operation_log_table_method = UserOperationLogTableMethod(conn, cur)
        user_operation_log_table_method.insert_user_operation_log(user_id, '修改用户信息', '修改用户信息成功')

        args_success = {
            "status": 1,
            "errorCode": None,
            "errorMsg": None,
            "resultBody": {
                "user_id": user_id,
                "real_name": real_name,
                "email": email,
                "address": address,
                "birthday": birthday,
                "sex": sex,
                "avatar": avatar
            }
        }


        return jsonify(args_success)
    
# 查询用户信息扩展
@sign_up_blueprint.route('/get_user_info_ext')
def get_user_info_ext():
    user_id = request.args.get('user_id')


    if not user_id:
        args_error = {
            "status": -1,
            "errorCode": 2005,
            "errorMsg": "args error",
            "resultBody": {}
        }
        return jsonify(args_error)
    
    with db_pool_singleton.getconn() as conn:
        cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)

        user_info_ext_table_method = UserInfoExtTableMethod(conn, cur)

        # 查询用户信息扩展
        user_info_ext = user_info_ext_table_method.select_user_info_ext(user_id)

        if not user_info_ext:
            args_error = {
                "status": -1,
                "errorCode": 2006,
                "errorMsg": "user not exists",
                "resultBody": {}
            }
            return jsonify(args_error)

        args_success = {
            "status": 1,
            "errorCode": None,
            "errorMsg": None,
            "resultBody": user_info_ext

        }


        return jsonify(args_success)




    